#include <QCoreApplication>
#include <QDebug>
#include <QFileInfo>
#include "osdeamonservice.h"
void outputMessage(QtMsgType type, const QMessageLogContext &, const QString &msg)
{
    QString text;
    switch(type)
    {
    case QtDebugMsg:
        text = QString("Debug:");
        break;
        
    case QtWarningMsg:
        text = QString("Warning:");
        break;
        
    case QtCriticalMsg:
        text = QString("Critical:");
        break;
        
    case QtFatalMsg:
        text = QString("Fatal:");
        
    case QtInfoMsg:
        text = QString("Info:");
    }
    
    QString current_date = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    QString message = current_date+ " " + text + " " + msg;
    QFile file("/opt/ota/os_daemon.log");
    
    if(!file.open(QIODevice::WriteOnly | QIODevice::Append))
    {
        qDebug() << "opne failed:" << file.fileName();
    }
    QTextStream text_stream(&file);
    text_stream << message << "\r\n";
    
    file.flush();
    file.close();
}

const QDateTime buildDateTime()
{
    QString dateTime;
    dateTime.clear();
    dateTime += __DATE__;
    dateTime += __TIME__;
    dateTime.replace("  "," 0"); //注意" "是两个空格，用于日期为单数时需要转成“空格+0”
    return QLocale(QLocale::English).toDateTime(dateTime,"MMM dd yyyyhh:mm:ss");
}
void showVersion(){
#ifdef ESPORT_BOX
    qDebug()<<"< os_daemon now version:espoet box!";
#else
    qDebug()<<"< os_daemon now version:cloud needle cloud!";
#endif
}
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QFileInfo fileInfo("/opt/ota/os_daemon.log");
    if(fileInfo.size() > 2*1024*1024)
        QFile::remove("/opt/ota/os_daemon.log");
    // 日志文件注册
    qInstallMessageHandler(outputMessage);
    // 编译版本 时间
    QString buildtime = buildDateTime().toString("yyyy.MM.dd hh:mm:ss");
    QCoreApplication::setApplicationVersion("v1.0, buildTime " + buildDateTime().toString("yyyy.MM.dd hh:mm:ss"));
    qDebug() << "---------------" << "build time:"<<buildtime << "---------------";
    showVersion();
    OSDeamonService osDeamonService;
    osDeamonService.start();
    
    return a.exec();
}
